package com.example.za.account.activity;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.example.za.account.R;
import com.example.za.account.application.AccountApplication;
import com.example.za.account.dao.UserDao;
import com.example.za.account.utils.L;
import com.example.za.account.utils.T;
import com.example.za.account.view.PasswordEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;


public class PasswordActivity extends BaseActivity {

    @BindView(R.id.et_modify_oldpass)
    PasswordEditText etModifyOldpass;
    @BindView(R.id.et_modify_newpass)
    PasswordEditText etModifyNewpass;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        ButterKnife.bind(this);
        setTitle(getString(R.string.modify_password));
        showBackwardView(true);
    }

    @Override
    protected Activity getSubActivity() {
        return this;
    }

    @OnClick(R.id.btn_modify_pass)
    public void onViewClicked() {
        final String oldPass = etModifyOldpass.getText().toString().trim();
        final String newPass = etModifyNewpass.getText().toString().trim();
        if (!TextUtils.isEmpty(oldPass) && !TextUtils.isEmpty(newPass)) {
            new AsyncTask<Void, Void, Void>() {

                @Override
                protected Void doInBackground(Void... params) {
                    BmobQuery<BmobUser> query = new BmobQuery<BmobUser>();
                    query.addWhereEqualTo("email", AccountApplication.sUser.geteMail());
                    query.findObjects(new FindListener<BmobUser>() {
                        @Override
                        public void done(List<BmobUser> list, BmobException e) {
                            if (e != null) {
                                T.showShort(getApplicationContext(), getString(R.string.check_net_connect));
                            } else {
                                if (list != null && list.size() > 0) {
                                    //     BmobUser bmobUser = list.get(0);
                                    BmobUser.updateCurrentUserPassword(oldPass, newPass, new UpdateListener() {
                                        @Override
                                        public void done(BmobException e) {
                                            if (e != null) {
                                                T.showShort(getApplicationContext(), getString(R.string.update_pass_fail));
                                            } else {
                                                new UserDao(PasswordActivity.this).updatePass(newPass, AccountApplication.sUser.getmId());
                                                T.showShort(getApplicationContext(), getString(R.string.update_pass_succeed));
                                                finish();
                                            }
                                        }
                                    });
                                } else {
                                    T.showShort(getApplicationContext(), getString(R.string.old_pass_mistake));
                                }
                            }
                        }
                    });
                    return null;
                }

            }.execute();
        } else {
            T.showShort(this, getString(R.string.input_full_info));
        }

    }

}
